<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称">{{ spuName }}</el-form-item>
      <el-form-item style="width:200px" label="SKU名称">
        <el-input v-model="skuInfo.skuName" placeholder="sku名称" />
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input v-model="skuInfo.price" type="number" placeholder="价格(元)" />
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input v-model="skuInfo.weight" placeholder="重量(千克)">1</el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input v-model="skuInfo.skuDesc" type="textarea" rows="4">1</el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form inline label-width="80px">
          <el-form-item v-for="attr in AttrInfoList" :key="attr.id" :label="attr.attrName">
            <el-select v-model="attr.attrIdAndValueId" size="mini" placeholder="请选择">
              <el-option v-for="attrValue in attr.attrValueList" :key="attrValue.id" :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form ref="form" :inline="true" label-width="80px">
          <el-form-item v-for="(saleAttr) in spuSaleAttrList" :key="saleAttr.id" :label="saleAttr.saleAttrName">
            <el-select v-model="saleAttr.attrIdAndValueId" placeholder="请选择">
              <el-option v-for="(saleAttrValue) in saleAttr.spuSaleAttrValueList" :key="saleAttrValue.id" :label="saleAttrValue.saleAttrValueName" :value="`${saleAttr.id}:${saleAttrValue.id}`" />
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table :data="SpuImageList" style="width:100%" border @selection-change="handleSelectionChange">
          <el-table-column type="selection" prop="prop" label="label" width="80" />
          <el-table-column label="图片" width="width">
            <template slot-scope="{row}">
              <img :src="row.imgUrl" alt="" style="width:100px;height:100px">
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width" />
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row}">
              <el-button v-if="row.isDefault==0" type="primary" @click="changeDefault(row)">设为默认</el-button>
              <el-button v-else type="success" plain>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'SKUFORM',
  data() {
    return {
      AttrInfoList: [], // 平台属性
      spuSaleAttrList: [], // 销售属性
      SpuImageList: [], // 图片
      spuName: '',
      skuInfo: {
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        price: '',
        weight: '',
        skuName: '',
        skuDefaultImg: '',
        skuDesc: '',
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   attrName: 'string',
          //   id: 0,
          //   skuId: 0,
          //   valueId: 0,
          //   valueName: 'string'
          // }
        ],
        skuImageList: [
          // {
          //   id: 0,
          //   imgName: 'string',
          //   imgUrl: 'string',
          //   isDefault: 'string',
          //   skuId: 0,
          //   spuImgId: 0
          // }
        ],
        skuSaleAttrValueList: [
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: 'string',
          //   saleAttrValueId: 0,
          //   saleAttrValueName: 'string',
          //   skuId: 0,
          //   spuId: 0
          // }
        ]
      },
      // 收集图片的数据字段:但是需要注意，收集的数据目前缺少isDefault字段，将来提交给服务器数据的时候，需要整理参数
      imageList: []
    }
  },
  methods: {
    getData(category1Id, category2Id, row) {
      // 收集父组件传递过来的数据
      this.spuName = row.spuName
      this.skuInfo.category3Id = row.category3Id
      this.skuInfo.spuId = row.id
      this.skuInfo.tmId = row.tmId

      // 请求平台属性
      this.$API.spu.reqGetAttrInfoList(category1Id, category2Id, row.category3Id).then(res => {
        if (res.code === 200) {
          this.AttrInfoList = res.data
        }
      })
      // 请求销售属性
      this.$API.spu.reqGetSaleAttrList(row.id).then(res => {
        if (res.code === 200) {
          this.spuSaleAttrList = res.data
        }
      })
      // 请求图片
      this.$API.spu.reqGetSpuImageList(row.id).then(res => {
        if (res.code === 200) {
          // isDefault
          const list = res.data
          list.forEach(item => {
            item.isDefault = 0
          })
          this.SpuImageList = list
        }
      })
    },
    // 排他操作
    changeDefault(row) {
      // 图片列表的数据的isDefault字段变为 0
      this.SpuImageList.forEach(item => {
        item.isDefault = 0
      })
      // 点击的那个图片的数据变为1
      row.isDefault = 1
      // 收集默认图片的地址
      this.skuInfo.skuDefaultImg = row.imgUrl
    },
    // table表格复选框按钮的事件
    handleSelectionChange(params) {
      // 获取到用户选中图片的信息数据，但是需要注意，当前收集的数据当中，缺少isDefault字段
      this.imageList = params
    },
    // 取消添加操作
    cancel() {
      // 清理数据
      Object.assign(this._data, this.$options.data())

      this.$emit('changeScene', 0)
    },
    // 保存操作
    save() {
      // 整理参数
      // AttrInfoList: [], // 平台属性
      // spuSaleAttrList: [], // 销售属性
      // SpuImageList: [], // 图片

      this.AttrInfoList.forEach(item => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(':')
          this.skuInfo.skuAttrValueList.push({ attrId, valueId })
        }
      })
      this.spuSaleAttrList.forEach(item => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] = item.attrIdAndValueId.split(':')
          this.skuInfo.skuSaleAttrValueList.push({ saleAttrId, saleAttrValueId })
        }
      })
      this.skuInfo.skuImageList = this.imageList.map(item => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id
        }
      })
      // 发送请求
      this.$API.spu.reqSaveSkuInfo(this.skuInfo).then(res => {
        if (res.code === 200) {
          this.$message({ type: 'success', message: '添加SKU成功' })
          this.cancel()
        }
      })
    }
  }
}
</script>

<style>
</style>
